<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<div class="uc-nav">
    <ul>
        <li class="active"><a href="javascript:;">全部评价</a></li>
        <li ref="1"><a href="javascript:;">已评价</a></li>
        <li ref="0"><a href="javascript:;">未评价</a></li>
    </ul>

</div>

<div class="ucr-centent">
    <table class="table table-border">
        <colgroup>
            <col width="20%">
            <col width="20%">
            <col width="13%">
            <col width="15%">
            <col width="10%">
            <col width="10%">
            <col width="12%">
        </colgroup>
        <thead>
        <tr>
            <th>编号</th>
            <th>名称</th>
            <th>办理部门</th>
            <th>时间</th>
            <th>评价</th>
            <th>评价</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody id="casetbody"></tbody>
    </table>
    <div style="text-align: center;">
        <div id="page" class="pagination"></div>
    </div>
</div>
<div class="modal fade evaluate-modal">
    <div class="modal-dialog">
        <form id="saform" class="form-horizontal" role="form">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">
                        <span aria-hidden="true">&times;</span><span class="sr-only">Close</span>
                    </button>
                    <h4 class="modal-title">我要评价</h4>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <label class="col-sm-3 control-label"><span class="text-red">*</span>评价等级：</label>
                        <div class="col-sm-8">
                            <label class="radio-inline">
                                <input name="grade" type="radio" value="0"> 非常满意
                            </label>
                            <label class="radio-inline">
                                <input name="grade" id="test" type="radio" value="1"> 满意
                            </label>
                            <label class="radio-inline">
                                <input name="grade" type="radio" value="3"> 一般
                            </label>
                            <label class="radio-inline">
                                <input name="grade" type="radio" value="2"> 不满意
                            </label>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">评价内容：</label>
                        <div class="col-sm-8">
                            <textarea name="content" class="form-control" id="content" rows="3"></textarea>
                            <p class="text-red">(您还可以输入200个字符)</p>
                        </div>
                    </div>

                </div>
                <div class="modal-footer">
                    <div class="btn-box">
                        <button type="button" id="submit" onclick="submitForm();" class="btn btn-yellow btn-lg">提交</button>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>
<script>
    $(function(){
        $(".uc-nav li").each(function(){
            $(this).click(function(){
                $(".active").removeClass("active");
                $(this).addClass("active");
                callBackPagination($(this).attr("ref"));
            })
        })
        callBackPagination("");
        $('.evaluate-modal').on('hide.bs.modal', function () {
            clearSaForm();
        })
    });

    var pjlb = {0:'办件',1:'咨询'};
    var pjzt = {0:'未评价',1:'已评价'};
var list=[];
    function callBackPagination(caseType,pi,ps) {
        var limit = ps||8;
        var currPage = pi||1;
        var userid = "${current_member.guid}";
        $.ajax({
            url: '<c:url value="/satisfaction/case-list"></c:url>',
            type:"get",
            data : {"ps" : limit,"pi":currPage,"sb":"ctm","sd":"desc","q_status_s_eq":caseType,"q_createuser_s_eq":userid},
            dataType: "json",
            success: function (res) {
                if(res.succ){
                    list=res.data.list;
                }
                $("#casetbody").html("");
                if(res.data.list==null||res.data.list.length==0){
                    $("#casetbody").html("<tr><td colspan='7'>暂未找到您的数据</td></tr>");
                }
                $.each( res.data.list, function(index, obj){
                    var type = pjlb[obj.stype];
                    var status = pjzt[obj.status];
                    var dohtml = '';
                    if(obj.status=="0"){
                        dohtml = '<td><a href="javascript:;" class="btn btn-xs btn-green" onclick="doSatisfaction('+index+');">评价</a></td>';
                    }else if(obj.status=="1"){
                        dohtml = '<td><a href="javascript:;" class="btn btn-xs btn-blue" onclick="showSatisfaction(\''+obj.guid+'\');">查看</a></td>';
                    }
                    var html='<tr><td>'+obj.bh+'</td><td>'+obj.title+'</td><td>'+obj.depName+'</td>'
                            +'<td>'+obj.ctm+'</td><td>'+type+'</td><td>'+status+'</td>'
                            + dohtml
                            +'</tr>';
                    $("#casetbody").append(html);
                });

                $("#page").jqPaginator({
                    totalPages: res.data.totalPage==0?1:res.data.totalPage,
                    visiblePages: 10,
                    currentPage: currPage,
                    pageSize: limit,
                    onPageChange: function (n,type) {
                        if(type=="change"){
                            getList(caseType,n,ps);
                        }
                    }
                });
            },
            error:function(resp){
                toast("服务器异常");
            }
        });

    }

    /**
     * 评价
     */
    var comment={};
    function doSatisfaction(index){
        $('.evaluate-modal').modal("show");
        comment=list[index];
    }

    /**
     * 展示评价内容
     */
    function showSatisfaction(guid){

        $.ajax({
            url: '<c:url value="/satisfaction/show-satisfaction/"></c:url>'+guid,
            type:"get",
            dataType: "json",
            success: function (res) {
                if(res.succ){
                    var data = res.data;
                    $("input[name='grade'][value='"+data.grade+"']").prop("checked",'checked');
                    $("#content").val(data.content);
                    $("#submit").attr("disabled",true);
                    $('.evaluate-modal').modal('show');
                }else{
                    toast(res.msg)
                }
            },
            error:function(resp){
                toast("服务器异常");
            }
        });
    }

    /**
     * 提交评价内容
     */
    function submitForm(){
        var satisfaction={};
        satisfaction.guid=comment.guid;
        satisfaction.stype=comment.stype;
        satisfaction.refId=comment.refGuid;
        satisfaction.grade=$("input[name='grade']:checked").val();
        satisfaction.content=$("#content").val();
        satisfaction.source="1";
        satisfaction.creatId="${current_member.guid}";
        satisfaction.creater="${current_member.realName}";
        if(satisfaction.guid){
            toast("不能重复评价");
            return;
        }
        if(satisfaction.grade==null){
            toast("评价等级不能为空");
            return;
        }
        $.ajax({
            url: '<c:url value="/satisfaction/do-satisfaction"></c:url>',
            type:"post",
            dataType: "json",
            contentType: 'application/json;charset=utf-8',
            data : JSON.stringify(satisfaction),
            success: function (res) {
                if(res.succ){
                    toast("评价成功");
                    $('.evaluate-modal').modal('hide');
                    callBackPagination($(".active").attr("ref"));
                }else{
                    toast(res.msg);
                }
            },
            error:function(resp){
                toast("服务器异常");
            }
        });
    }

    /**
     * 清空评价表单
     */
    function clearSaForm(){
        $("#submit").attr("disabled",false);
        $("#content").val('');
        $("input[name='grade']").removeAttr("checked");
    }
</script>